package com.dragon.mmochat.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.dragon.mmochat.model.enumType.MmochatPlayerShopState;
import com.dragon.mmochat.model.enumType.MmochatPlayerShopType;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.dragon.mmochat.model.shop.MmochatPlayerShop;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MmochatShopDao extends SimpleJdbcDaoSupport {
	private static final ShopRowMapper shopRowMapper = new ShopRowMapper();

	// 读取所有商店
	private static final String getAllShopsSql = "select * from shop "
			+ "where id >= ? and id < ? and state != 3";

	public List<MmochatPlayerShop> getAllShops(int startId, int endId) {
		return getSimpleJdbcTemplate().query(getAllShopsSql, shopRowMapper,
				startId, endId);
	}

	// 创建新商店ID
	private static final String createNewShopIdSql = "select nextval('shop_id_seq'::regclass)";

	public int createNewShopId() {
		return getSimpleJdbcTemplate().queryForInt(createNewShopIdSql);
	}

	// 创建新商店
	private static final String createNewShopSql = "INSERT INTO shop(id, roleid, name, type, "
			+ "grids, state, role_name, shop_money) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

	public void createNewShop(int id, int roleId, String name, int type,
			String grids, int state, String roleName, long shop_money) {
		getSimpleJdbcTemplate().update(createNewShopSql, id, roleId, name,
				type, grids, state, roleName, shop_money);
	}

	// 更新商店柜台
	private static final String updateShopSql = "update shop set grids = ?, page_num = ? where id = ?";

	public void updateShop(String grids, int pageNum, int id) {
		getSimpleJdbcTemplate().update(updateShopSql, grids, pageNum, id);
	}

	// 增加商店金钱
	private static final String addShopMoneySql = "update shop set shop_money = shop_money + ? where id = ?";

	public void addShopMoney(int add, int id) {
		getSimpleJdbcTemplate().update(addShopMoneySql, add, id);
	}

	// 修改商店状态
	private static final String editShopStateSql = "update shop set state = ? where id= ?";

	public void editShopState(int state, int id) {
		getSimpleJdbcTemplate().update(editShopStateSql, state, id);
	}

	// 修改商店公告
	private static final String editShopIntroSql = "update shop set intro = ? where id= ?";

	public void editShopIntro(String intro, int id) {
		getSimpleJdbcTemplate().update(editShopIntroSql, intro, id);
	}

	// 出售商店/取消出售
	private static final String sellShopSql = "update shop set state = ?, sell_price = ? where id= ?";

	public void sellShop(int state, int price, int id) {
		getSimpleJdbcTemplate().update(sellShopSql, state, price, id);
	}

	// 更改商店所有人
	private static final String updateShopOwnerSql = "update shop set roleid = ?, state = ?, "
			+ "sell_log = null, role_name = ?, sell_price = 0 where id= ?";

	public void updateShopOwner(int roleid, int state, String role_name, int id) {
		getSimpleJdbcTemplate().update(updateShopOwnerSql, roleid, state,
				role_name, id);
	}

	// 出售商店物品
	private static final String updateShopGridAndMoneySql = "update shop set grids = ?, "
			+ "shop_money = shop_money + ?, sell_log = ?, score = score + ? where id = ?";

	public void updateShopGridAndMoney(String grids, int addMoney,
			String sellLog, int winScore, int id) {
		getSimpleJdbcTemplate().update(updateShopGridAndMoneySql, grids,
				addMoney, sellLog, winScore, id);
	}

	// 商店扣租金
	private static final String updateShopDailyPaySql = "update shop set shop_money = shop_money - ?,"
			+ "state = ?, score = score - ? where id = ?";

	public void updateShopDailyPay(int del, int state, int delScore, int id) {
		getSimpleJdbcTemplate().update(updateShopDailyPaySql, del, state,
				delScore, id);
	}

	// 获取所有商店的资金总和
	private static final String getTotalSmallMoneySql = "select sum(shop_money) from shop";

	public long getTotalSmallMoney() {
		return getSimpleJdbcTemplate().queryForLong(getTotalSmallMoneySql);
	}

	static class ShopRowMapper implements
			ParameterizedRowMapper<MmochatPlayerShop> {
		@Override
		public MmochatPlayerShop mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MmochatPlayerShop shop = new MmochatPlayerShop();
			shop.setId(rs.getInt("id"));
			shop.setRoleId(rs.getInt("roleid"));
			shop.setRoleName(rs.getString("role_name"));
			shop.setShopType(MmochatPlayerShopType.get(rs.getInt("type")));
			shop.setName(rs.getString("name"));
			shop.setScore(rs.getLong("score"));
			shop.setPageNum(rs.getInt("page_num"));
			shop.setState(MmochatPlayerShopState.get(rs.getInt("state")));
			shop.setShopMoney(rs.getLong("shop_money"));
			shop.setIntro(rs.getString("intro"));
			shop.setSell_price(rs.getInt("sell_price"));

			Gson gson = new Gson();
			String str = rs.getString("grids");
			if (str != null) {
				Map<Integer, MmochatPackageGrid> grids = gson.fromJson(str,
						new TypeToken<Map<Integer, MmochatPackageGrid>>() {
						}.getType());
				if (grids != null) {
					shop.setGrids(grids);
				}
			}

			str = rs.getString("sell_log");
			if (str != null) {
				List<String> logs = gson.fromJson(str,
						new TypeToken<List<String>>() {
						}.getType());
				if (logs != null) {
					for (String log : logs) {
						shop.addSellLog(log);
					}
				}
			}

			return shop;
		}
	}
}
